package com.ctrip.ec.api.mobile.controller;

import com.ctrip.ec.biz.dao.ad.AdDao;
import com.ctrip.ec.biz.dao.ad.AdPositionDao;
import com.ctrip.ec.biz.entity.ad.AdEntity;
import com.ctrip.ec.biz.entity.ad.AdPositionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jiangzy on 2019/1/8.
 */

@RestController
@RequestMapping("ad")
public class adController {

    @Autowired
    private AdDao ad;

    @Autowired
    private AdPositionDao adPosition;

    @Value("${imageDomain}")
    private String imageDomain;

    @GetMapping("topList")
    public List<AdEntity> topList() throws SQLException {
        AdPositionEntity adPositionEntity = adPosition.entityByCode("home-banner");
        if(adPositionEntity != null){
            List<AdEntity> list = ad.list("position_id=" + adPositionEntity.getId());
            for(AdEntity entity:list){
                entity.setImageUrl(this.imageDomain + entity.getImageUrl());
            }
            return list;
        }else{
            return new ArrayList<>();
        }

    }
}
